//
//  YiDaLoginViewController.swift
//  YiDa
//
//  Created by zhoufei on 16/6/26.
//  Copyright © 2016年 ZhouFei. All rights reserved.
//

import UIKit
typealias loginSuccessBlock = () ->Void
class YiDaLoginViewController: UIViewController {

    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var tel: UITextField!
    var isRegisterSuccess:Bool = false
    var loginSuccess:loginSuccessBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func register(sender: AnyObject) {
        
        let register = self.storyboard!.instantiateViewControllerWithIdentifier("register") as! YiDaRegisterViewController
        register.registerSuccess = {() in
            self.isRegisterSuccess = true
        }
        self.presentViewController(register, animated: true, completion: nil)
    }
    @IBAction func login(sender: AnyObject) {

        
        self.view.endEditing(true)
        if PublicFunction.checkByRegex(TEL_PATTEN, text: self.tel.text!){
            if self.password.text?.characters.count >= 6{
                //登陆
                let param:Dictionary<String,AnyObject> = ["tel":self.tel.text!,"password":self.password.text!]
                    YiDaNet.main.request(LOGIN, param: param, completionHandler: { (inner) in
                        do{
                            let result = try inner()
                            if result["error_code"]?.intValue == 200 {
                                YiDaUser.main.tel = self.tel.text!
                                YiDaUser.main.password = self.password.text!
                                YiDaUser.main.userId = result["userId"]?.intValue
                                YiDaUser.main.saveDefault()
                                if self.loginSuccess != nil{
                                    self.loginSuccess!()
                                }
                                self.dismissViewControllerAnimated(true, completion: nil)
                            }else{
                                self.view.makeToast(String(result["error_msg"]))
                            }
                        }catch let error{
                            print(error)
                            
                        }
                    })
                }else{
                    self.view.makeToast("密码长度不能小于6位")
                }

        }else{
            self.view.makeToast("电话号码输入有误")
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if self.isRegisterSuccess{
            self.view.makeToast("注册成功，请登陆")
            self.isRegisterSuccess = false
        }
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
